﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TarreOA.TarreOABLL;
using TarreOA.TarreOAModels;

public partial class admin_Flow_BackManage_UpdateFlowManager : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            IList<TarreOA.TarreOAModels.User> list = UserManager.FindAll();
            Flow_NewFlow fnf = Flow_NewFlow_Service.FindByFlowId(Convert.ToInt32(Request.QueryString["id"]));
            string[] flowManagers = fnf.NewFlow_manager.Split(',');
            foreach (TarreOA.TarreOAModels.User u in list)
            {
                int state = 0;
                foreach (string flowManager in flowManagers)
                {
                    if (u.LoginName == flowManager)
                    {
                        lbSellManager.Items.Add(new ListItem("[" + u.Section.SectionName + "]" + u.UserName, u.LoginName));
                        state = 1;
                        break;
                    }
                }
                if (state==0)
                {
                    lbSellUser.Items.Add(new ListItem("[" + u.Section.SectionName + "]" + u.UserName, u.LoginName));
                }
            }
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        lbSellManager.Items.Clear();
        lbSellUser.Items.Clear();
        IList<TarreOA.TarreOAModels.User> list = UserManager.FindAll();
        Flow_NewFlow fnf = Flow_NewFlow_Service.FindByFlowId(Convert.ToInt32(Request.QueryString["id"]));
        string[] flowManagers = fnf.NewFlow_manager.Split(',');
        if (ddlSelectType.SelectedValue=="0")
        {
            foreach (TarreOA.TarreOAModels.User u in list)
            {
                int state = 0;
                foreach (string flowManager in flowManagers)
                {
                    if (u.LoginName == flowManager)
                    {
                        lbSellManager.Items.Add(new ListItem("[" + u.Section.SectionName + "]" + u.UserName, u.LoginName));
                        state = 1;
                        break;
                    }
                }
                if (state == 0)
                {
                    lbSellUser.Items.Add(new ListItem("[" + u.Section.SectionName + "]" + u.UserName, u.LoginName));
                }
            }
        }
        else
        {
            foreach (TarreOA.TarreOAModels.User u in list)
            {
                int state = 0;
                foreach (string flowManager in flowManagers)
                {
                    if (u.LoginName == flowManager)
                    {
                        lbSellManager.Items.Add(new ListItem("[" + u.PopedomGroup.PopedomGroupName + "]" + u.UserName, u.LoginName));
                        state = 1;
                        break;
                    }
                }
                if (state == 0)
                {
                    lbSellUser.Items.Add(new ListItem("[" + u.PopedomGroup.PopedomGroupName + "]" + u.UserName, u.LoginName));
                }
            }
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string[] flowManagerstrs = Request.Form.GetValues("lbSellManager");
        string flowManagerstr = "";
        if (flowManagerstrs != null)
        {
            foreach (string var in flowManagerstrs)
            {
                flowManagerstr += var + ",";
            }
        }
        Flow_NewFlow nf = new Flow_NewFlow();
        nf.NewFlowl_id = Convert.ToInt32(Request.QueryString["id"]);
        nf.NewFlow_manager = flowManagerstr;
        if (Flow_NewFlow_Service.UpdateFlowManager(nf))
        {
            lbSellManager.Items.Clear();
            lbSellUser.Items.Clear();
            IList<TarreOA.TarreOAModels.User> list = UserManager.FindAll();
            Flow_NewFlow fnf = Flow_NewFlow_Service.FindByFlowId(Convert.ToInt32(Request.QueryString["id"]));
            string[] flowManagers = fnf.NewFlow_manager.Split(',');
            if (ddlSelectType.SelectedValue == "0")
            {
                foreach (TarreOA.TarreOAModels.User u in list)
                {
                    int state = 0;
                    foreach (string flowManager in flowManagers)
                    {
                        if (u.LoginName == flowManager)
                        {
                            lbSellManager.Items.Add(new ListItem("[" + u.Section.SectionName + "]" + u.UserName, u.LoginName));
                            state = 1;
                            break;
                        }
                    }
                    if (state == 0)
                    {
                        lbSellUser.Items.Add(new ListItem("[" + u.Section.SectionName + "]" + u.UserName, u.LoginName));
                    }
                }
            }
            else
            {
                foreach (TarreOA.TarreOAModels.User u in list)
                {
                    int state = 0;
                    foreach (string flowManager in flowManagers)
                    {
                        if (u.LoginName == flowManager)
                        {
                            lbSellManager.Items.Add(new ListItem("[" + u.PopedomGroup.PopedomGroupName + "]" + u.UserName, u.LoginName));
                            state = 1;
                            break;
                        }
                    }
                    if (state == 0)
                    {
                        lbSellUser.Items.Add(new ListItem("[" + u.PopedomGroup.PopedomGroupName + "]" + u.UserName, u.LoginName));
                    }
                }
            }
            Response.Redirect("Flow_NewFlowList.aspx");
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "alert('保存失败！请重试！')", true);
        }
    }
}
